'use strict';

const Controller = require('egg').Controller;

class OrderController extends Controller {
  async getOrders() {
    const { service, ctx } = this;
    const result = await service.order.getOrders();
    return ctx.cc(result);
  }
  async upDateOrder() {
    const { service, ctx } = this;
    const { good_id, num } = ctx.query;
    const result = await service.order.upDateOrder(good_id, num);
    if (result.affectedRows === 1) {
      return ctx.cc();
    }
    return ctx.err();
  }
  async deleteOrder() {
    const { service, ctx } = this;
    const ids = ctx.request.body.good_ids;
    const result = await service.order.deleteOrder(ids);
    ctx.cc(result);
  }
  async getOrder() {
    const { service, ctx } = this;
    const { id } = ctx.params;
    const result = await service.order.getOrder(id);
    ctx.cc(result);
  }
  async addOrder() {
    const { service, ctx } = this;
    const { good_id, num } = ctx.request.body;
    const result = await service.order.addOrder(good_id, num);
    if (result.affectedRows === 1) {
      return ctx.cc();
    }
    return ctx.err();
  }
}

module.exports = OrderController;
